package com.qianfeng.openapi.web.master.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianfeng.openapi.web.master.dto.TableData;
import com.qianfeng.openapi.web.master.dto.TableParam;
import com.qianfeng.openapi.web.master.entity.UserToken;
import com.qianfeng.openapi.web.master.feign.CacheServiceClient;
import com.qianfeng.openapi.web.master.mapper.UserTokenMapper;
import com.qianfeng.openapi.web.master.service.UserTokenService;
import com.qianfeng.openapi.web.master.utils.SysConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/12/30
 * @Time: 下午5:08
 */
@Service
public class UserTokenServiceImpl implements UserTokenService {

    @Resource
    private UserTokenMapper userTokenMapper;

    @Resource
    private CacheServiceClient cacheServiceClient;


    @Override
    public TableData findTokenByPage(TableParam param) {
        //list  range  3 6
        //IPage<T> page, @Param("ew") Wrapper<T> queryWrapper
        IPage iPage = new Page();
        //(nowPage-1)*pageSize = offset
        int current = param.getOffset()/param.getLimit()+1;
        iPage.setCurrent(current);
        iPage.setSize(param.getLimit());
        ((Page) iPage).setSearchCount(true);

        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(param.getSearch())){
            queryWrapper.like("access_token",param.getSearch());
        }


        iPage = userTokenMapper.selectPage(iPage,queryWrapper);

        TableData data = new TableData();
        data.setRows(iPage.getRecords());
        data.setTotal(iPage.getTotal());
        return data;
    }

    @Override
    public R save(UserToken userToken) {
        int i = userTokenMapper.insert(userToken);

        if (i>0){

            cacheServiceClient.
                    set(SysConstant.TOKEN_KEY+userToken.getAccessToken(), JSON.toJSONString(userToken));

        }
        return i>0?R.ok(null):R.failed("");
    }

    @Override
    public R findTokenById(Integer id) {
        UserToken userToken = userTokenMapper.selectById(id);
        return  R.ok(userToken);
    }

    @Override
    public R update(UserToken userToken) {
        //跟新前
        UserToken old = userTokenMapper.selectById(userToken.getId());

        int i = userTokenMapper.updateById(userToken);//
        System.out.println(userToken+"---"+i);

        if (i>0){
            System.out.println("----->同步更新缓存");

           //原来存储的清除
            System.out.println("---->"+old);
            cacheServiceClient.delKey(SysConstant.TOKEN_KEY+old.getAccessToken());
            //新的存储到redis
            cacheServiceClient.
                    set(SysConstant.TOKEN_KEY+userToken.getAccessToken(), JSON.toJSONString(userToken));
        }

        return i>0?R.ok(null):R.failed("");
    }

    @Override
    public R deleteBatch(List<Integer> ids) {
        List<UserToken> list = userTokenMapper.selectBatchIds(ids);

        int i = userTokenMapper.deleteBatchIds(ids);
        if(i>0){

        for (UserToken userToken : list) {
            cacheServiceClient.delKey(SysConstant.TOKEN_KEY+userToken.getAccessToken());
        }
        }


        return i>0?R.ok(null):R.failed("");
    }
}
